#include "MenuSystem.h"
#include "IrControl.h"
#include <fcntl.h>
#include "Menu/Tool/frame.h"

#define ITEM_TEST_TOUCH_ESC (0)
#define TEST_TEST_TOUCH_QTY   (1)
#define  TOUCH_KEY 0XFF


TPosCoor TestTouchItemPos[TEST_TEST_TOUCH_QTY] = { {400,400} };
TRect		TestTouchTouchRect[TEST_TEST_TOUCH_QTY];
char		*TestTouchInfoStr[TEST_TEST_TOUCH_QTY] = {"ESC"};
TPosCoor	TestTouchPos[5]= { {40,40}, {640,40}, {40,400}, {640,420}, {340,220} };
TPosCoor	TestTouchInfoPos ={190,140};
int		TestTouchFocus = -1;
TFrame	TestTouchFrame;
static TPosCoor	Touch_clbr_pos[5];
static unsigned char touch_clbr_flag ; ///20080217
extern	unsigned char read_info_data[10];
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern int TS_KX; ////20080217
extern int TS_KY;
extern int TP_CLBR_C_LX ;//logic x
extern int TP_CLBR_C_LY ;
extern pthread_cond_t dt_cond;


void Disp_TouchCalibrate (TCanvas *pCanvas,int idx)
{
	char FileName[20];
	
	DrawJpeg(TestTouchFrame.pCanvas, 0, 0, "/logo.jpg" );
	g_FillRect( TestTouchFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_WHITE);		
	
	sprintf( FileName, "%d/%d.jpg", TEST_TOUCH_FOLDER, IMG_TEST_TPTEST_FOCUS);
	DrawJpeg(pCanvas,TestTouchPos[idx].x, TestTouchPos[idx].y, FileName);
	
	sprintf( FileName, "%d/%d.jpg", TEST_TOUCH_FOLDER, IMG_TEST_TPTEST_INFOCOVER);
	DrawJpeg(pCanvas, TestTouchInfoPos.x, TestTouchInfoPos.y, FileName);	

	TestTouchDrawItem( TestTouchFrame.pCanvas, ITEM_TEST_TOUCH_ESC, FALSE );
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);	
	//dt_Present();	
}

int TouchCalibrate (TPosCoor pos)
{	
	int offer_x = 0,offer_y = 0;
	int temp_x = 0,temp_y = 0;
	long temp_kx,temp_ky;

	switch (touch_clbr_flag)
		{
		case	0:
			temp_x = 60;
			temp_y = 60;
			break;
		case	1:
			temp_x = 660;
			temp_y = 60;
			break;
		case	2:
			temp_x = 60;
			temp_y = 420;
			break;
		case	3:
			temp_x = 660;
			temp_y = 420;
			break;
		case	4:
			temp_x = 360;
			temp_y = 240;
			break;
		}

	 				
	offer_x = ((pos.x-temp_x >0) ? (pos.x-temp_x) : (-1 *(pos.x-temp_x)) ) ;
	offer_y = ((pos.y-temp_y >0) ? (pos.y-temp_y) : (-1 *(pos.y-temp_y)) );
		
	
	
	if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
	 {
	 		switch (temp_x)
	 		{
	 			case 60:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[0].x = pos.x;
	 						Touch_clbr_pos[0].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[2].x = pos.x;
	 						Touch_clbr_pos[2].y = pos.y;
	 						TP_CLBR_C_LX = pos.x;
	 						TP_CLBR_C_LY = pos.y;
	 					}
	 				break;
	 			case 660:
	 				if (pos.y <100)
	 					{
	 						Touch_clbr_pos[1].x = pos.x;
	 						Touch_clbr_pos[1].y = pos.y;
	 					}
	 				else
	 					{
	 						Touch_clbr_pos[3].x = pos.x;
	 						Touch_clbr_pos[3].y = pos.y;
	 					}
	 				break;
	 			case 360:	 				
	 						Touch_clbr_pos[4].x = pos.x;
	 						Touch_clbr_pos[4].y = pos.y;	 					
	 				break;
	 		} 	 	
	 	}
	 	else
	 		{
	 			printf ("Too much offer!!\n");
	 			return -1;		
	 		}
	 		
	 	if ( Touch_clbr_pos[4].x != 0)
	 	{		
	 		temp_kx = ((Touch_clbr_pos[1].x-Touch_clbr_pos[0].x)*100/(TP_CLBR_B_X-TP_CLBR_A_X)+(Touch_clbr_pos[3].x-Touch_clbr_pos[2].x)*100/(TP_CLBR_D_X-TP_CLBR_C_X))/2;
	 		temp_ky = ((Touch_clbr_pos[2].y-Touch_clbr_pos[0].y)*100/(TP_CLBR_C_Y-TP_CLBR_A_Y)+(Touch_clbr_pos[3].y-Touch_clbr_pos[1].y)*100/(TP_CLBR_D_Y-TP_CLBR_B_Y))/2;
	 		temp_x = Touch_clbr_pos[2].x + (temp_kx*(TP_CLBR_E_X - TP_CLBR_C_X))/100;
	 		temp_y = Touch_clbr_pos[2].y + (temp_ky*(TP_CLBR_E_Y - TP_CLBR_C_Y))/100;
	 		
			offer_x = (Touch_clbr_pos[4].x-temp_x >0) ? (Touch_clbr_pos[4].x-temp_x) : (-1 *(Touch_clbr_pos[4].x-temp_x)) ;
			offer_y = (Touch_clbr_pos[4].y-temp_y >0) ? (Touch_clbr_pos[4].y-temp_y) : (-1 *(Touch_clbr_pos[4].y-temp_y)) ;

			if ((offer_x < TP_CLBR_OFFER ) && (offer_x < TP_CLBR_OFFER) )
			{
				printf ("  center is calulate ok!!\n");
				printf ("temp_kx: %ld ,temp_ky is: %ld temp_x: %d,temp_y: %d\n",temp_kx,temp_ky,temp_x,temp_y);							
				memset(Touch_clbr_pos, 0, sizeof(Touch_clbr_pos));	
				touch_clbr_flag = 0;				
				return 1;
			}	
			else
				{
				printf ("  center is calulate error!!\n");		
				touch_clbr_flag = 0;//////////////maybe can be changed.20080708
				return -1;		
				}			
		}
		return 0;	
}


int TestTouchGetTouchID( TPosCoor pos )
{
	int i;		
	
	char FileName[20];
	int GetJpegIdx;	
	TRect Rect;
	TJpeg pJpeg;
	int rc;

	for( i = 0; i < TEST_TEST_TOUCH_QTY; i++ )
		if( ut_PtInRect( pos, TestTouchTouchRect[i] ) )
		{				
			return i;
		}

	GetJpegIdx = IMG_TEST_TPTEST_FOCUS;
	sprintf( FileName, "%d/%d.jpg", TEST_TOUCH_FOLDER, GetJpegIdx);
	pJpeg = GetJpegPtr(FileName);

	Rect.left = TestTouchPos[touch_clbr_flag].x ;//- TP_CLBR_OFFER;
	Rect.top = TestTouchPos[touch_clbr_flag].y;
	Rect.right= TestTouchPos[touch_clbr_flag].x +pJpeg.Width;
	Rect.bottom= TestTouchPos[touch_clbr_flag].y+pJpeg.Height;

	rc = TouchCalibrate(pos);
	if(rc == -1)//ut_PtInRect( pos, Rect ) ) //all is success.
		return -1;
	if (rc ==1)
		{
			TestMainInit(1);				
			return -1;
		}
	else
		{
			touch_clbr_flag++; 
			//Disp_TouchCalibrate(VideoTPAdjFrame.pCanvas,touch_clbr_flag);
			return TOUCH_KEY;
		}
	//return 0; //check it 20090105
		
	return -1;
}

void TestTouchDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= TEST_TEST_TOUCH_QTY )
		return;

	if( bfocus ){
		GetJpegIdx = IMG_TEST_TPTEST_ITEM_FOCUS_ESC+idx ;
	}else{
		GetJpegIdx = IMG_TEST_TPTEST_ITEM_ESC+idx ;
	}
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	pos = TestTouchItemPos[idx];
	sprintf( FileName, "%d/%d.jpg", TEST_TOUCH_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	TestTouchTouchRect[idx].left = pos.x;
	TestTouchTouchRect[idx].right = pos.x + pJpeg.Width;
	TestTouchTouchRect[idx].top = pos.y;
	TestTouchTouchRect[idx].bottom = pos.y + pJpeg.Height;	
	g_Puts( pCanvas, FONT33, TestTouchTouchRect[idx], TEXT_ALIGN_CENTER, TestTouchInfoStr[idx] );
	
}





BOOL TestTouchBtnEnter(int idx)
{
	if (idx <0)
		return TRUE;
	switch(idx)
	{
		case ITEM_TEST_TOUCH_ESC:
			TestMainInit(1);				
			break;
	}
	return TRUE;
}

void TestTouchDraw( BOOL bDrawAll )
{
      int i;
	printf (" in TestTouchDraw ~~\n");
	g_FillRect( TestTouchFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_WHITE);		
	touch_clbr_flag =0;
	memset(Touch_clbr_pos, 0, sizeof(Touch_clbr_pos));	
	Disp_TouchCalibrate (TestTouchFrame.pCanvas,touch_clbr_flag);

	for( i = 0; i < TEST_TEST_TOUCH_QTY; i++ )
		TestTouchDrawItem( TestTouchFrame.pCanvas, i, FALSE );

	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	
}


void TestTouchInit()
{
	ir_SetCurrentID( IR_TEST_TOUCH);
	frm_Create( &TestTouchFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	TestTouchDraw( TRUE );
	printf ("in test main Init ~~\n");
	
}


void TestTouchInput(UInt32 key)
{

	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				TestTouchFocus = TestTouchGetTouchID( pos );
				if(  (TestTouchFocus  > 0) && (TestTouchFocus  <TEST_TEST_TOUCH_QTY) )
				{
					TestTouchDrawItem( TestTouchFrame.pCanvas, TestTouchFocus, TRUE );
					dt_Present_part(TestTouchTouchRect [TestTouchFocus]);
				}
				else
					if  (TestTouchFocus  == TOUCH_KEY)
					{
					Disp_TouchCalibrate(TestTouchFrame.pCanvas,touch_clbr_flag);					
					TestTouchFocus = -1;
					}
			}
		}
		else
		{
			if( ( focus = TestTouchGetTouchID( pos ) ) == TestTouchFocus )
				TestTouchBtnEnter( TestTouchFocus );
			
			TestTouchFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}


		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( TestTouchFocus = TestTouchGetTouchID( pos )) != -1 )
				{
					Disp_TouchCalibrate(TestTouchFrame.pCanvas,touch_clbr_flag);					
					TestTouchFocus = -1;
				}
			}
		}
		LastKey = key;//hangsheng add 071126
	}
	
}






